package com.glodon.paas.document.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.glodon.paas.activity.api.bean.Activity;
import com.glodon.paas.activity.client.ActivityClient;
import com.glodon.paas.consts.StringConst;
import com.glodon.paas.document.api.bean.StringUtil;
import com.glodon.paas.document.web.util.SecurityUtils;

@Controller
public class ActivityController {

    @Autowired
    ActivityClient activityClient;

    /** 
     * 
     * 默认时间倒序
     * 可用参数 
     * @param receiverId   必须，接受者ID
     * @param appId
     * @param moduleId
     * @param activityType
     * @param itemId
     * @param itemType
     * @param subItemId
     * @param actorId       发送者ID
     * @param earlierTime   较新的时间,毫秒
     * @param laterTime     较早的时间,毫秒
     * @param count         需要查询记录数
     * @param needRender    是否 需要使用模板渲染：值可以是true，false，1，0
     * @return
     */
    @RequestMapping(value = "activity", method = RequestMethod.GET)
    public ModelAndView query(
                    HttpServletRequest request, 
                    @RequestParam(value = "receiverId", required = false) String receiverId,
                    @RequestParam(value = "moduleId", required = false) String moduleId,
                    @RequestParam(value = "activityType", required = false) String activityType,
                    @RequestParam(value = "itemId", required = false) String itemId,
                    @RequestParam(value = "itemType", required = false) String itemType,
                    @RequestParam(value = "subItemId", required = false) String subItemId,
                    @RequestParam(value = "actorId", required = false) String actorId,
                    @RequestParam(value = "earlierTime", required = false) String earlierTime,
                    @RequestParam(value = "laterTime", required = false) String laterTime,
                    @RequestParam(value = "count", required = false, defaultValue = "50") String count,
                    @RequestParam(value = "needRender", required = false, defaultValue = "true")Boolean needRender,
                    ModelMap model
                    ) {
        String userId = SecurityUtils.getCurrentUser().getId();
        if(StringUtil.isBlank(receiverId)){
            receiverId = userId;
        }

        Map<String, String> params = new HashMap<String, String>();
        params.put("receiverId", receiverId);
        params.put("appId", String.valueOf(activityClient.getAppId()));
        params.put("moduleId", moduleId);
        params.put("activityType", activityType);
        params.put("itemId", itemId);
        params.put("itemType", itemType);
        params.put("subItemId", subItemId);
        params.put("actorId", actorId);
        params.put("earlierTime", earlierTime);
        params.put("laterTime", laterTime);
        params.put("count", count);
        params.put("needRender", String.valueOf(needRender));

        Activity[] results = activityClient.query(params);
        model.addAttribute(StringConst.ITEMS, results);
        return new ModelAndView("activity", model);
    }
}
